package cn.zhaotianen.codegenerator.tools;

/**
 * <p/>
 * <pre>
 * ***************************************************************
 *  Copyright (c) 2014-2015 –苏州犀牛网络科技有限公司
 *  Package: PACKAGE_NAME
 *  Description:
 *  @since 1.0.0
 *  @author 赵天恩
 *  @date 2017/2/20
 *  @time 16:50
 * ***************************************************************
 * </pre>
 */
public class OtherTool {

    public static void main(String[] args) {
        String string="TENANT_ID";
        StringBuffer result=new StringBuffer();
        string = string.toLowerCase();
        String[] regex= string.split("_");
        if (regex.length!=1){
           for (int i=0;i<regex.length;i++){
               if (i==0){
                   result.append(regex[i]);
               }else {
                   result.append(captureName(regex[i]));
               }
           }


        }
        System.out.println(result.toString());
    }

    /**
     * 首字母大写
     * @param name
     * @return
     */
    public static String captureName(String name) {
        char[] cs=name.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }

    /**
     * 格式转换，形如：
     * "TENANT_ID"--->"tenantId",
     * "ID"--->"id"
     *
     * @param string
     * @return
     */
    public static String stringFormat(String string, Boolean needFormatFirst) {
        StringBuffer result = new StringBuffer();
        if (string == null || string.equals("")) {
            return "";
        } else {
            string = string.toLowerCase();
            String[] regex = string.split("_");
            if (regex.length != 1) {
                for (int i = 0; i < regex.length; i++) {
                    if (i == 0) {
                        if (needFormatFirst) {
                            result.append(captureName(regex[i]));
                        } else {
                            result.append(regex[i]);
                        }

                    } else {
                        result.append(captureName(regex[i]));
                    }
                }
            } else {
                if (needFormatFirst) {
                    result.append(captureName(regex[0]));
                } else {
                    result.append(regex[0]);
                }
            }
            return result.toString();
        }
    }

}
